package com.ht.sps.nh.module;

import com.ht.sps.web.view.MimeView;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.UploadAdaptor;
import org.nutz.web.ajax.Ajax;
import org.nutz.web.ajax.AjaxReturn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@IocBean
@At("nh/system/setting")
public class SettingModule {
    private static final Logger log = LoggerFactory.getLogger(SettingModule.class);

    /**
     * 主界面入口
     * @param request
     */
    @At("/")
    @Ok("th:nh/system/setting")
    public void index(HttpServletRequest request){
        log.debug("主函数入口测试");

    }

    @At
    @Ok("fastjson")
    @AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
    public AjaxReturn uploadPhoto(HttpServletRequest request, @Param("viewFiles") File file,@Param("num")Integer num){
        File fJpg = new File(request.getServletContext().getRealPath("/resources/nh/images/"+num+".jpg"));
        File fPng = new File(request.getServletContext().getRealPath("/resources/nh/images/"+num+".png"));
        fJpg.delete();
        fPng.delete();
        String path = request.getServletContext().getRealPath("/resources/nh/images/"+num+"."+file.getName().substring(file.getName().length()-3));
        try {
            Files.copyFile(file, new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Ajax.ok();
    }

    @At
    @Ok("fastjson")
    public AjaxReturn delPhoto(HttpServletRequest request,@Param("num")Integer num){
        File fJpg = new File(request.getServletContext().getRealPath("/resources/nh/images/"+num+".jpg"));
        File fPng = new File(request.getServletContext().getRealPath("/resources/nh/images/"+num+".png"));
        if(fJpg.exists()||fPng.exists()){
            try {
                fJpg.delete();
                fPng.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return Ajax.ok();
        }else{
            return Ajax.fail();
        }
    }

    @At
    public View png(HttpServletRequest request,@Param("photoName") String photoName) {
        String path = null;
        if(photoName!=null&&!photoName.equals("")){
            String jpgPath = request.getServletContext().getRealPath("/resources/nh/images/"+photoName+".jpg");
            String pngPath = request.getServletContext().getRealPath("/resources/nh/images/"+photoName+".png");
            if (new File(jpgPath).exists()) {
                path = jpgPath;
            } else if (new File(pngPath).exists()) {
                path = pngPath;
            }
        }

        if (path == null) {
            path = request.getServletContext().getRealPath("/resources/nh/images/default.jpg");
        }

        return new MimeView(path);
    }
}